package br.cesed.lti.ementol.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;

import br.cesed.lti.ementol.beans.Periodo;
import br.cesed.lti.ementol.dao.util.HibernateFactory;

import cc.co.taciano.webbase.Fachada;
import cc.co.taciano.webbase.dao.DAOException;
import cc.co.taciano.webbase.dao.Dao;

/**
 * @author Taciano Morais Silva
 * @version 23/01/2009, 12h11m
 * @since 21/10/2008, 12h00m
 */
public class PeriodoDao extends AbstractDao<Periodo> implements Dao<Periodo> {

    public PeriodoDao(Fachada fachada) throws DAOException {
        super(fachada);
    }

    /**
     *
     * @throws br.cesed.lti.cesuweb.dao.util.DAOException
     */
    public PeriodoDao() throws DAOException {
        super();
    }

    /**
     * @see br.cesed.lti.cesuweb.dao.Dao#create(br.cesed.lti.cesuweb.beans.Bean)
     */
    @Override
    public void create(Periodo bean) throws DAOException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    /**
     * Find an Event by its primary key.
     *
     * @param id
     * @return
     * @see br.cesed.lti.cesuweb.dao.Dao#find(java.lang.Integer)
     */
    @Override
    public Periodo find(Integer id) throws DAOException {
        return (Periodo) super.find(Periodo.class, id);
    }

    /**
     * @see br.cesed.lti.cesuweb.dao.Dao#findAll()
     */
    @Override
    public List<Periodo> findAll() throws DAOException {
        return super.findAll(Periodo.class);
    }

    /**
     * @see br.cesed.lti.cesuweb.dao.Dao#update(br.cesed.lti.cesuweb.beans.Bean)
     */
    @Override
    public void update(Periodo bean) throws DAOException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    /**
     * @see br.cesed.lti.cesuweb.dao.AbstractDao#delete(br.cesed.lti.cesuweb.beans.Bean)
     */
    @Override
    public void delete(Periodo obj) throws DAOException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    /**
     * Delete a detached Event from the database.
     *
     * @param id
     */
    public void delete(Integer id) throws DAOException {
        super.delete(Periodo.class, id);
    }

    /**
     * @return
     * @throws DAOException
     */
    public Periodo getPeriodoAtivo() throws DAOException {
        Periodo periodo = null;
        try {
            startOperation();
            Criteria criteria = session.createCriteria(Periodo.class);
            criteria.add(Restrictions.eq("status", Periodo.ATIVO));
            periodo = (Periodo) criteria.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return periodo;
    }
}
